/**
 * Copyright 2011 Marco Berri - marcoberri@gmail.com
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 *
 */
package com.mbfasttrack.controller;

import com.mbfasttrack.model.Comment;
import com.mbfasttrack.model.Like;
import com.mbfasttrack.service.ActionService;
import com.mbfasttrack.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

/**
 * @author Marco Berri marcoberri@gmail.com http://tecnicume.blogspot.com
 */
@Controller
public class CommentController extends Base {

    /**
     *
     * @return
     */
    @ModelAttribute("comment")
    public Comment getCommentObject() {
        return new Comment();
    }

    /**
     *
     * @param commentService
     */
    @Autowired(required = true)
    public void setCommentService(CommentService commentService) {
        this.commentService = commentService;

    }

    /**
     *
     * @param actionService
     */
    @Autowired(required = true)
    public void setActionService(ActionService actionService) {
        this.actionService = actionService;

    }

    /**
     * Dettaglio del tracciato
     *
     * @param id
     * @param model
     * @return
     */
    @RequestMapping(value = "/comment/like/{id}", method = RequestMethod.GET)
    public String addLike(@PathVariable String id, Model model) {

        if (isGuest()) {
            return "redirect:/login";
        }

        Comment comment = commentService.commentRepository.findOne(id);

        org.springframework.security.core.userdetails.User u = (org.springframework.security.core.userdetails.User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();


        Like l = likeService.add(comment, u.getUsername(), Boolean.TRUE);
        actionService.add(u.getUsername(), comment, userService.userRepository.findOneByUsername(u.getUsername()));

        return "response/ok";
    }

    /**
     *
     * @param comment
     * @param model
     * @return
     */
    @RequestMapping(value = "/comment/save", method = RequestMethod.POST)
    public String save(@ModelAttribute("comment") Comment comment, Model model) {

        if (isGuest()) {
            return "redirect:/login";
        }

        org.springframework.security.core.userdetails.User u = (org.springframework.security.core.userdetails.User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        comment = this.commentService.add(comment, u.getUsername());

        //aggancio la eventuale traccia
        if (comment.getObjtype().equalsIgnoreCase("track")) {
            comment.setObject(trackService.trackRepository.findOne(comment.getObjid()));
        } else if (comment.getObjtype().equalsIgnoreCase("group")) {
            comment.setObject(groupService.groupRepository.findOne(comment.getObjid()));

        } else if (comment.getObjtype().equalsIgnoreCase("user")) {
            comment.setObject(userService.userRepository.findOne(comment.getObjid()));

        } else if (comment.getObjtype().equalsIgnoreCase("comment")) {
            Comment parent = commentService.commentRepository.findOne(comment.getObjid());
            comment.setObject(parent);
            comment.setParent(parent);

        }


        commentService.save(comment);

        actionService.add(u.getUsername(), comment, comment.getObject());

        model.addAttribute("comment", comment);
        return "response/ok";

    }
}